import xlwings as xw
from pyecharts.charts import Map

from pyecharts import options as opts
from pyecharts.charts import Bar, Timeline, Page
from pyecharts.charts import WordCloud
from pyecharts.globals import SymbolType

page = Page(layout=Page.DraggablePageLayout)
wb = xw.Book('global.xlsx')
sht1 = wb.sheets["data"]
data = sht1.range('A1:CA186885').value

title = data[0]
year_index = title.index('iyear')
country_index = title.index('country_txt')
kill_index = title.index('nkill')
name_index = title.index('gname')
attack_type_index = title.index('attacktype1_txt')
target_type_index = title.index('targsubtype1_txt')
weapon_index = title.index('weapsubtype1_txt')

print(attack_type_index)
print(weapon_index)
print(target_type_index)

data.remove(title)

country_death = {}
country_time = {}

name_list = []
value_list = []
name_item = {}

attack_type_dict = {}
target_type_dict = {}
weapon_type_dict = {}
name_dict = {}
for i in range(1970, 2019):

    for j in data:
        year = j[year_index]
        name = j[name_index]
        if int(year) == i:
            name_dict[name] = name_dict.get(name, 0) + 1

    list1 = list(name_dict.items())
    list1.sort(key=lambda x: x[1], reverse=True)
    x = []
    y = []
    if len(list1) >= 10:
        list_length = 10
    else:
        list_length = len(list1)

    for k in range(list_length):
        word, number = list1[k]
        x.append(word)
        y.append(number)
    name_list.append(x)
    value_list.append(y)

for i in data:
    country = i[country_index]
    country_time[country] = country_time.get(country, 0) + 1
    if i[kill_index]:
        kill = int(i[kill_index])
        country_death[country] = country_death.get(country, 0) + kill

    attack_type = i[attack_type_index]
    attack_type_dict[attack_type] = attack_type_dict.get(attack_type, 0) + 1

    weapon_type = i[weapon_index]
    weapon_type_dict[weapon_type] = weapon_type_dict.get(weapon_type, 0) + 1

    target_type = i[target_type_index]
    target_type_dict[target_type] = target_type_dict.get(target_type, 0) + 1

data1 = []
for key, value in country_death.items():
    data1.append([key, value])
data2 = []
for key, value in country_time.items():
    data2.append([key, value])
c = (
    Map()
        .add("全球分布", data1, "world")
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        .set_global_opts(
        title_opts=opts.TitleOpts(title="1970-2018全球死亡人数分布"),
        visualmap_opts=opts.VisualMapOpts(max_=200),

    )

)
# c.render('死亡人数全球分布.html')
page.add(c)
m = (
    Map()
        .add("全球分布", data2, "world")
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        .set_global_opts(
        title_opts=opts.TitleOpts(title="1970-2018全球恐怖袭击数分布"),
        visualmap_opts=opts.VisualMapOpts(max_=200),

    )

)

# m.render('袭击次数全球分布.html')
page.add(m)
tl = Timeline()
for i in range(1970, 2018):
    name_list[i - 1970].reverse()
    value_list[i - 1970].reverse()
    bar = (
        Bar()
            .add_xaxis(name_list[i - 1970][0:9])
            .add_yaxis("恐怖组织", value_list[i - 1970][0:9])
            .reversal_axis()
            .set_global_opts(title_opts=opts.TitleOpts("恐怖组织{}年袭击次数".format(i)),
                             legend_opts=opts.LegendOpts(pos_left="60%", pos_top='5%'),
                             )

    )
    tl.add(bar, "{}年".format(i))
tl.add_schema(play_interval=500)
# tl.render("竞赛.html")
page.add(tl)
words = []
for key, value in attack_type_dict.items():
    words.append((key, value))
c = (
    WordCloud()
        .add("", words, word_size_range=[20, 100], shape=SymbolType.DIAMOND)
        .set_global_opts(title_opts=opts.TitleOpts(title="攻击类型词云图"))
    # .render("攻击类型词云图.html")
)
page.add(c)
words = []
for key, value in weapon_type_dict.items():
    words.append((key, value))
c = (
    WordCloud()
        .add("", words, word_size_range=[20, 100], shape=SymbolType.DIAMOND)
        .set_global_opts(title_opts=opts.TitleOpts(title="武器类型词云图"))
    # .render("武器类型词云图.html")
)
page.add(c)
words = []
for key, value in target_type_dict.items():
    words.append((key, value))
c = (
    WordCloud()
        .add("", words, word_size_range=[20, 100], shape=SymbolType.DIAMOND)
        .set_global_opts(title_opts=opts.TitleOpts(title="目标类型词云图"))
    # .render("目标类型词云图.html")
)
page.add(c)
page.render('echarts.html')
